package org.ielts.action;

import static com.yifeng.struts2.RequestParameters.*;
import static com.yifeng.struts2.ResultParameters.*;
import static com.yifeng.struts2.ResultConstants.*;
import static com.yifeng.struts2.ActionUtils.*;

import org.apache.commons.lang.StringUtils;
import org.ielts.domain.security.Role;
import org.ielts.domain.security.User;
import org.ielts.profile.IeltsProfile;
import org.ielts.service.security.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import com.yifeng.ProfileContext;
import com.yifeng.modules.annotations.Operation;
import com.yifeng.profile.Profile;
import com.yifeng.sitemap.Catalog;
import com.yifeng.utils.JsonResultBuilder;

public class WelcomeAction extends BaseAction {

	@Operation(result = FILL)
	public String main() {

		return FILL;
	}

	@Operation(result = GOTO)
	public String changeCampus() {
		String campus = form.readString("campus");

		if (StringUtils.isNotBlank(form.readString("first"))) {

			User user = (User) Profile.getInstance().getUser();
			// 课程顾问不考虑校区
			boolean isConsultant = false;
			for (Role role : user.getRoles()) {
				if (role.getName().equals("课程顾问")) {
					isConsultant = true;
					break;
				}
			}

			if (isConsultant) {
				campus = null;
			}
		}

		IeltsProfile profile = (IeltsProfile) Profile.getInstance().getProfile();
		profile.setCampus(campus);

		setLocation("main");
		return GOTO;
	}

	@Autowired
	UserService userService;

	@Operation(result = JSON)
	public String doSetUserCampus() {
		String campus = form.readString("campus");

		userService.setUserCampus(campus);

		setJson(JsonResultBuilder.success(true).msg("设置默认校区成功").json());

		return JSON;
	}
}
